package cn.xny.org.controller;


import cn.xny.basic.util.AjaxResult;
import cn.xny.basic.util.PageList;
import cn.xny.org.domain.Department;
import cn.xny.org.query.DepartmentQuery;
import cn.xny.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@Api(value = "部门接口类", description = "处理部门的CRUD操作")
@RestController
@RequestMapping("/department")
public class DepartmentController {


    @Autowired
    private DepartmentService departmentService;


    /**
     * 查询单个
     */

    @ApiOperation(value = "查询单个")
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id") Long id) {


        return departmentService.loadByAll(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @ApiOperation(value = "查询全部")
    @GetMapping
    public List findAll() {

        return departmentService.loadAll();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {


            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");

        }

    }

    /**
     * 添加或者修改
     */
    @ApiOperation(value = "添加或者修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {

                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();

            return new AjaxResult(false, "操作失败");
        }

    }

    /**
     * 分页加高级查询
     *
     * @param departmentQuery
     * @return
     */
    @ApiOperation(value = " 分页加高级查询")
    @PostMapping
    public PageList<Department> pageQuery(@RequestBody DepartmentQuery departmentQuery) {

        return departmentService.pageQuery(departmentQuery);

    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");

        }

    }
    @ApiOperation(value = "查询全部")
    @GetMapping("/deptTree")
    public List<Department>  deptTree() {

        return departmentService.deptTree();
    }

}
